package com.singzi.javaSenior.threads.dataStructure;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

public class SetTest {
    public static void main(String[] args) throws InterruptedException {
        List<String> unSafeSet = new ArrayList<>();
        List<String> safeSet1 = Collections.synchronizedList(new ArrayList<>());
        CopyOnWriteArrayList<String> safeSet2 = new CopyOnWriteArrayList<>();
        ListThread t1 = new ListThread(unSafeSet);
        ListThread t2 = new ListThread(safeSet1);
        ListThread t3 = new ListThread(safeSet2);

        for(int i = 0; i < 10; i++){
            Thread t = new Thread(t1, String.valueOf(i));
            t.start();
        }
        for(int i = 0; i < 10; i++) {
            Thread t = new Thread(t2, String.valueOf(i));
            t.start();
        }
        for(int i = 0; i < 10; i++) {
            Thread t = new Thread(t3, String.valueOf(i));
            t.start();
        }

        //等待子线程执行完
        Thread.sleep(2000);

        System.out.println("listThread1.list.size() = " + t1.list.size());
        System.out.println("listThread2.list.size() = " + t2.list.size());
        System.out.println("listThread3.list.size() = " + t3.list.size());

        //输出list中的值
        System.out.println("unsafeList：");
        for(String s : t1.list){
            if(s == null){
                System.out.print("null  ");
            }
            else
            {
                System.out.print(s + "  ");
            }
        }
        System.out.println();
        System.out.println("safeList1：");
        for(String s : t2.list){
            if(s == null){
                System.out.print("null  ");
            }
            else
            {
                System.out.print(s + "  ");
            }
        }
        System.out.println();
        System.out.println("safeList2：");
        for(String s : t3.list){
            if(s == null){
                System.out.print("null  ");
            }
            else
            {
                System.out.print(s + "  ");
            }
        }
    }
}
class ListThread implements Runnable{
    public List<String> list;

    public ListThread(List<String> list){
        this.list = list;
    }

    @Override
    public void run() {
        int i = 0;
        while(i<10)
        {
            try {
                Thread.sleep(100);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            //把当前线程名称加入list中
            list.add(Thread.currentThread().getName());
            i++;
        }
    }
}
